package com.ms.p2p.controller.admin;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ms.common.utils.MsDateUtils;
import com.ms.common.utils.excel.ExcelData;
import com.ms.p2p.bean.PageBean;
import com.ms.p2p.controller.BaseController;
import com.ms.p2p.provider.MemberAccountRecordProvider;
import com.ms.p2p.utils.JsonDataGridHelper;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 用户资金明细记录
 */
@Controller("AdminAccountDetailController")
@Transactional
@RequestMapping({"/admin/account_log"})
public class AdminAccountDetailController extends AdminBaseController {

    @Reference
    private MemberAccountRecordProvider logService;

    @RequestMapping("/list")
    public String list() {
        return "admin/account/record/accountlog/list";
    }
    @RequestMapping(value = {"/queryData"}, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public PageBean queryData( String userId, String userName,
                               String account_type, String begin_date,
                               String end_date,
                              PageBean page
    ) {

        page.setList(logService.findPager(userId, userName,
                account_type, begin_date, end_date, page.getBegin(),
                page.getPageSize()));
        page.setTotalCount(logService.findCount(userId, userName, account_type,
                begin_date, end_date));
        return JsonDataGridHelper.createJSONData(page);
    }


    @RequestMapping("/exportlist")
    public String exportAccountLogList(
            String userId, String userName,
            String account_type, String begin_date,
            String end_date, Model model) {

        List<Map<String, Object>> logs = logService.findPager(userId, userName,
                account_type, begin_date, end_date, 0, null);

        for (Map<String, Object> map : logs) {

            Object total = map.get("total");
            if (total != null) {
                map.put("total", total.toString());
                BigDecimal t = BigDecimal.ZERO;
                t = t.add(BigDecimal.valueOf(Double.valueOf(String.valueOf(total))));
                Object collection = map.get("collection");
                if (collection != null) {
                    t = t.add(BigDecimal.valueOf(Double.valueOf(String.valueOf(collection.toString()))));
                }
                map.put("total", t.doubleValue() + "");
            }
            Object money = map.get("money");
            if (money != null) {
                map.put("money", money.toString());
            }
            Object balance = map.get("balance");
            if (balance != null) {
                map.put("balance", balance.toString());
            }
            Object freezing_amont = map.get("freezing_amont");
            if (freezing_amont != null) {
                map.put("freezing_amont", freezing_amont.toString());
            }
            Object collection = map.get("collection");
            if (collection != null) {
                map.put("collection", collection.toString());
            }
            String addtime = (String) map.get("addtime");
            if (addtime != null) {
                map.put("addtime", MsDateUtils.TimeStamp2DateTime(addtime));
            }
        }

        ExcelData excelData = new ExcelData();
        excelData.setHeads(new String[]{"用户ID", "用户名称", "记录编号", "记录类型",
                "账户总额", "操作金额", "可用余额", "冻结金额", "待收金额", "记录时间", "记录备注"});
        excelData.setFieldNames(new String[]{"user_id", "userName", "id",
                "logName", "total", "money", "balance", "freezing_amont",
                "collection", "addtime", "remark"});
        excelData.setDataList(logs);
        excelData.setModelClass(Map.class);
        model.addAttribute("excelData", excelData);
        model.addAttribute("fileName", "资金使用记录");
        return "excel";
    }

    @Override
    public boolean getBinderFlag() {
        return true;
    }
}
